package com.example.administrator.waitan.find.food;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.waitan.R;
import com.example.administrator.waitan.base.BaseActivity;
import com.example.administrator.waitan.find.FindFragment;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/12.
 */
public class FoodActivity extends BaseActivity {
    private RequestQueue queue;
    private ListView foodListView,sweetListView,healthListView;
    private FoodBaseAdapter foodBaseAdapter;
    private SweetBaseAdapter sweetBaseAdapter;
    private HealthBaseAdapter healthBaseAdapter;
    private List<FoodInfo> foodInfos;
    private List<SweetInfo> sweetInfos;
    private List<HealthInfo> healthInfos;
    private String foodUrl = "http://rm.app.hongniujia.com/json_rm.php?action=list&ver=4&package=com.aslibra.wt.meishiversion=1.0page=0&category=meishi&category2=%E5%88%9B%E6%84%8F%E7%BE%8E%E9%A3%9F&pageSize=24&client=android&appCookie=";
    private String sweetURL = "http://rm.app.hongniujia.com/json_rm.php?action=list&ver=4&package=com.aslibra.wt.meishiversion=1.0page=0&category=meishi&category2=%E7%83%98%E7%84%99%E7%94%9C%E7%82%B9&pageSize=24&client=android&appCookie=";
    private String healthUrl = "http://rm.app.hongniujia.com/json_rm.php?action=list&ver=4&package=com.aslibra.wt.meishiversion=1.0page=0&category=meishi&category2=%E6%BB%8B%E8%A1%A5%E5%85%BB%E7%94%9F&pageSize=24&client=android&appCookie=";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food);
        foodListView = (ListView) findViewById(R.id.lv_food_food);
        sweetListView = (ListView) findViewById(R.id.lv_food_sweet);
        healthListView = (ListView) findViewById(R.id.lv_food_health);
        queue = Volley.newRequestQueue(this);
        StringRequest foodRequest = new StringRequest(foodUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                foodInfos = new ArrayList<>();
                FoodBeans foodBeans = gson.fromJson(response, FoodBeans.class);
                for (int i = 0; i < foodBeans.getData().size(); i++) {
                    foodInfos.add(foodBeans.getData().get(i));
                }
                foodBaseAdapter = new FoodBaseAdapter(FoodActivity.this,foodInfos);
                foodListView.setAdapter(foodBaseAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        StringRequest sweetRequest = new StringRequest(sweetURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                sweetInfos = new ArrayList<>();
                SweetBeans sweetBeans = gson.fromJson(response, SweetBeans.class);
                for (int i = 0; i < sweetBeans.getData().size(); i++) {
                    sweetInfos.add(sweetBeans.getData().get(i));
                }
                sweetBaseAdapter = new SweetBaseAdapter(FoodActivity.this,sweetInfos);
                sweetListView.setAdapter(sweetBaseAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        StringRequest healthRequest = new StringRequest(healthUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                healthInfos = new ArrayList<>();
                HealthBeans healthBeans = gson.fromJson(response, HealthBeans.class);
                for (int i = 0; i < healthBeans.getData().size(); i++) {
                    healthInfos.add(healthBeans.getData().get(i));
                }
                healthBaseAdapter = new HealthBaseAdapter(FoodActivity.this,healthInfos);
                healthListView.setAdapter(healthBaseAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(healthRequest);
        queue.add(sweetRequest);
        queue.add(foodRequest);
    }
}
